package edu.neusoft.android.yuejian14.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import edu.neusoft.android.yuejian14.R;
import edu.neusoft.android.yuejian14.fragment.ActivityFragment;
import edu.neusoft.android.yuejian14.fragment.ClubFragment;
import edu.neusoft.android.yuejian14.fragment.MyFragment;
import edu.neusoft.android.yuejian14.fragment.PicFragment;
import edu.neusoft.android.yuejian14.fragment.TrainFragment;

public class MainActivity extends FragmentActivity implements OnClickListener
{
	private LinearLayout mTabWeixin;
	private LinearLayout mTabFrd;
	private LinearLayout mTabAddress;
	private LinearLayout mTabSettings;
	private LinearLayout mTabMy;

	private ImageButton mImgWeixin;
	private ImageButton mImgFrd;
	private ImageButton mImgAddress;
	private ImageButton mImgSettings;
	private ImageButton mImgMy;

	private Fragment mTab01;
	private Fragment mTab02;
	private Fragment mTab03;
	private Fragment mTab04;
	private Fragment mTab05;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);   //加载布局文件
		initView();     //初始化视图
		initEvent();    //初始化事件
		setSelect(0);
	}

	private void initEvent()
	{
		mTabWeixin.setOnClickListener(this);
		mTabFrd.setOnClickListener(this);
		mTabAddress.setOnClickListener(this);
		mTabSettings.setOnClickListener(this);
		mTabMy.setOnClickListener(this);
	}

	private void initView()            //初始化
	{
		mTabWeixin = (LinearLayout) findViewById(R.id.id_tab_weixin);   //对垂直线做事件监听，实现点击
		mTabFrd = (LinearLayout) findViewById(R.id.id_tab_frd);
		mTabAddress = (LinearLayout) findViewById(R.id.id_tab_address);
		mTabSettings = (LinearLayout) findViewById(R.id.id_tab_settings);
		mTabMy = (LinearLayout) findViewById(R.id.id_tab_my);

		mImgWeixin = (ImageButton) findViewById(R.id.id_tab_weixin_img);     //修改点击按钮的背景色
		mImgFrd = (ImageButton) findViewById(R.id.id_tab_frd_img);
		mImgAddress = (ImageButton) findViewById(R.id.id_tab_address_img);
		mImgSettings = (ImageButton) findViewById(R.id.id_tab_settings_img);
		mImgMy = (ImageButton) findViewById(R.id.id_tab_my_img);
	}

	private void setSelect(int i)
	{
		FragmentManager fm = getSupportFragmentManager();     //初始化
		FragmentTransaction transaction = fm.beginTransaction();
		hideFragment(transaction);

		switch (i)
		{
		case 0:
			if (mTab03 == null)
			{
				mTab03 = new ActivityFragment();
				transaction.add(R.id.id_content, mTab03);
			} else
			{
				transaction.show(mTab03);
			}
			mImgAddress.setImageResource(R.drawable.tab_activity_pressed);
			break;


		case 1:
			if (mTab02 == null)
			{
				mTab02 = new ClubFragment();transaction.add(R.id.id_content, mTab02);
			} else
			{
				transaction.show(mTab02);
				
			}
			mImgFrd.setImageResource(R.drawable.tab_club_pressed);
			break;
		case 2:
			if (mTab01 == null)
			{
				mTab01 = new TrainFragment();transaction.add(R.id.id_content, mTab01);   //add就是把刚刚初始化的fragment加入到可变化的界面上
			} else
			{
				transaction.show(mTab01);
			}
			mImgWeixin.setImageResource(R.drawable.tab_train_pressed);    //实现点击button变色
			break;
		case 3:
			if (mTab04 == null)
			{
				mTab04 = new PicFragment();transaction.add(R.id.id_content, mTab04);
			} else
			{
				transaction.show(mTab04);
			}
			mImgSettings.setImageResource(R.drawable.tab_pic_pressed);
			break;

			case 4:
				if (mTab05 == null)
				{
					mTab05 = new MyFragment();
					transaction.add(R.id.id_content, mTab05);
				} else
				{
					transaction.show(mTab05);
				}
				mImgMy.setImageResource(R.drawable.tab_my_pressed);
				break;

		default:
			break;
		}

		transaction.commit();
	}

	private void hideFragment(FragmentTransaction transaction)
	{
		if (mTab02 != null)
		{
			transaction.hide(mTab02);
		}
		if (mTab03 != null)
		{
			transaction.hide(mTab03);
		}
		if (mTab04 != null)
		{
			transaction.hide(mTab04);
		}
		if (mTab05 != null)
		{
			transaction.hide(mTab05);
		}
		if (mTab01 != null)
		{
			transaction.hide(mTab01);
		}
	}

	@Override
	public void onClick(View v)
	{
		resetImgs();
		switch (v.getId())
		{
		case R.id.id_tab_address:
			setSelect(0);
			break;
		case R.id.id_tab_frd:
			setSelect(1);    //相当于fragment1
			break;
			case R.id.id_tab_weixin:
			setSelect(2);
			break;
		case R.id.id_tab_settings:
			setSelect(3);
			break;
			case R.id.id_tab_my:
			setSelect(4);
			break;

		default:
			break;
		}
	}


	private void resetImgs()
	{
		mImgWeixin.setImageResource(R.drawable.tab_train_normal);
		mImgFrd.setImageResource(R.drawable.tab_club_normal);
		mImgAddress.setImageResource(R.drawable.tab_activity_normal);
		mImgSettings.setImageResource(R.drawable.tab_pic_normal);
		mImgMy.setImageResource(R.drawable.tab_my_normal);
	}

}
